<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=8">
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title>我的画廊</title>
    <link rel="stylesheet" th:href="@{/layui/layui/css/layui.css}">
</head>
<body>
<div class="layui-main">
    <div class="demoTable" style="margin-top: 15px;">
        查询画廊：
        <div class="layui-inline">
            <input class="layui-input" name="id" id="demoReload" placeholder="请输入用户名" autocomplete="off">
        </div>
        <button class="layui-btn" data-type="reload" onclick="query()">查询</button>
    </div>
    <br />
    <table id="dates" lay-filter="test"></table>
</div>
<input type="hidden" id="domain" th:value="${config.getDomain()}" />
<script th:src="@{/static/js/jquery-3.4.1.min.js}" type="text/javascript"></script>
<script th:src="@{/layui/layui/layui.js}"></script>
    <script type="text/javascript">
        var tableIns = null;
        var domain = '';
        $.ajaxSetup({
            cache: false
        });
        var table = null;
        layui.use(['form','table','layer'], function(){
            table = layui.table;
            var layer = layui.layer;
            var form = layui.form;
            domain = $('#domain').val();
            tableIns = table.render({
                elem: '#dates'
                ,cache: false//关闭本地磁盘缓存，
                ,toolbar: '<div class="layui-btn-group">\n' +
                    '  <button type="button" lay-event="delall" class="layui-btn layui-btn-sm">\n' +
                    '    <i class="layui-icon">&#xe640;</i>删除选中\n' +
                    '  </button>\n' +
                    '</div>' //开启工具栏，此处显示默认图标，可以自定义模板，详见文档
                ,defaultToolbar:['filter', 'print']
                ,method:'post'
                ,url:'/admin/getAlbumURLList'
                ,limit:10 //默认为10
                ,cols: [ [
                    {type:'checkbox',width:40,}
                    ,{field:'albumtitle',width:300,  title: '画廊标题'}
                    ,{field:'albumkey', width:300, title: '画廊地址', templet: function(d){return domain+'/'+d.albumkey;}}
                    ,{field:'username',  title: '创建用户',templet: function(d){
                                var str = d.username;
                                if(d.username=='' || d.username==null){str='游客';}
                                return str;
                            }
                        }
                    ,{field:'createdate',  title: '创建时间',sort: true}
                    ,{field:'password', title: '访问密码',templet: function(d){
                            var str = d.password;
                            if(d.password=='' || d.password==null){str='无';}
                            return str;
                        }
                    }
                    ,{field:'level',width:70,  title: '操作',templet: function(d){
                            return '<div class="layui-btn-group">\n' +
                                '  <button type="button" class="layui-btn layui-btn-sm"  lay-event="del">\n' +
                                '    <i class="layui-icon">&#xe640;</i>\n' +
                                '  </button>\n' +
                                '</div>';
                        }
                    }

                ] ]
                ,page: true
                ,done:function(res, curr, count){
                    //数据的回调用，可不写
                }
            });
            // 行点击事件
            table.on('tool', function (obj) {
                var data = obj.data;
                var event = obj.event;
                if (event === 'del') {
                    var index = layer.confirm('确定删除这个画廊吗？', {
                        btn: ['删除','取消'] //按钮
                    }, function(){
                        $.ajax({
                            type: "POST",
                            url: "/admin/delAlbum",
                            async:false,
                            data: {albumkey: data.albumkey},
                            dataType: "json",
                            success: function (data) {
                                if (data > 0) {
                                    table.reload('dates');//重新获取表数据
                                    layer.msg('删除成功', {icon: 1});
                                }
                            }
                        });
                        layer.close(index);
                    }, function(){
                    });
                }else{
                    layer.msg('参数异常', {icon: 2});
                }
            });

            //头工具栏事件
            table.on('toolbar(test)', function(obj){
                var checkStatus = table.checkStatus(obj.config.id);
                switch(obj.event){
                    case 'delall':
                        var index = layer.confirm('确定删除选中画廊？', {
                            btn: ['删除','取消'] //按钮
                        }, function(){
                            var data = checkStatus.data;
                            var arr = new Array();
                            for(var i = 0;i<data.length;i++){
                                arr.push(data[i].albumkey)
                            }
                            delall(arr);
                            table.reload('dates');//重新获取表数据
                            layer.close(index);
                        }, function(){
                        });
                        //layer.alert(JSON.stringify(data));
                        break;
                };
            });

        });

    function query(){
        var data = $('#demoReload').val();
        //这里以搜索为例
        tableIns.reload({
            where: { //设定异步数据接口的额外参数，任意设
                username: data
            }
            ,page: {
                curr: 1 //重新从第 1 页开始
            }
        });
        }

        function delall(arr) {
            $.ajax({
                type: "POST",
                url: "/admin/delAlbumAll",
                data: {albumkeyArr: arr},
                dataType: "json",
                success: function (data) {
                    if (data > 0) {
                        layer.msg('删除成功', {icon: 1});
                    }
                }
            });
        }
    </script>
</body>
</html>